package cn.edu.mju.property.dao.impl;

import cn.edu.mju.property.dao.NoticeDao;
import cn.edu.mju.property.entity.Notice;
import cn.edu.mju.property.entity.Repair;
import cn.edu.mju.property.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class NoticeDaoImpl implements NoticeDao {


    @Override
    public boolean addNotice(Notice notice) {
        return false;
    }

    @Override
    public boolean delNotice() {
        return false;
    }

    @Override
    public boolean updateNotice(Notice notice) {
        return false;
    }

    @Override
    public List<Notice> findNoticeByPage(int start, int rows) {
        Notice notice = null;
        List<Notice> Noticelist = new ArrayList<Notice>();
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select * from notice limit ? , ? ";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,start);
            pstmt.setInt(2,rows);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()){
                notice = new Notice();
                notice.setId(rs.getInt("id"));
                notice.setTitle(rs.getString("title"));
                notice.setContent(rs.getString("content"));
                notice.setTime(rs.getString("time"));
                notice.setUsername(rs.getString("username"));
                Noticelist.add(notice);
            }
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Noticelist;
    }

    @Override
    public int findTotalCount() {
        try {
            Connection conn =  JDBCUtil.getConnect();
            String sql = "select count(*) from notice";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            int num =  rs.getInt("count(*)");
            pstmt.close();
            conn.close();
            return num;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
